﻿using System;
using System.Collections.Generic;
using __Able.Network.Packets;

namespace __Able.Network.Events
{
    public class ProcessorRouter
    {
        private List<IPacketListener> listeners;
        private Channel channel;
        public ProcessorRouter(Channel channel)
        {
            this.channel = channel;
            listeners = new List<IPacketListener>();
        }
        /// <summary>
        /// 装载包处理器
        /// </summary>
        /// <param name="listener">The listener.</param>
        public void InstallProcessor(IPacketListener listener)
        {
            listeners.Add(listener);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inPacket"></param>
        public void PacketArrived(_InPacket inPacket)
        {
            try
            {
                foreach (IPacketListener listener in listeners)
                {
                    if (listener.Accept(inPacket))
                    {
                        listener.PacketArrived(inPacket);
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorPacket errorPacket = new ErrorPacket(ErrorPacketType.RUNTIME_ERROR, channel.User, e);
            }
        }
    }
}
